import request from '@/utils/request'

// Mock data for development
const useMockData = true // Set to false to use real API endpoints

// Mock data functions
const getMockOverview = () => {
  return {
    code: 200,
    data: {
      pendingOrders: 12,
      todaySales: 5280.50,
      totalSales: 128750.75,
      totalOrders: 358,
      totalUsers: 125,
      totalProducts: 86,
      topProduct: {
        name: '高性能笔记本电脑',
        sales: 12580.00
      }
    },
    message: 'success'
  }
}

const getMockSalesTrend = () => {
  return {
    code: 200,
    data: {
      months: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
      values: [5000, 8000, 12000, 9000, 15000, 20000, 18000, 22000, 19000, 25000, 28000, 30000]
    },
    message: 'success'
  }
}

const getMockCategorySales = () => {
  return {
    code: 200,
    data: {
      values: {
        '电子产品': [5000, 8000, 12000, 9000, 15000, 20000, 18000, 22000, 19000, 25000, 28000, 30000],
        '服装': [3000, 4000, 6000, 5000, 7000, 9000, 8000, 10000, 9500, 11000, 12000, 15000],
        '食品': [2000, 3000, 4000, 3500, 5000, 6000, 5500, 7000, 6500, 8000, 9000, 10000],
        '家居': [1000, 1500, 2000, 1800, 2500, 3000, 2800, 3500, 3200, 4000, 4500, 5000]
      }
    },
    message: 'success'
  }
}

const getMockSalesPercentage = () => {
  return {
    code: 200,
    data: {
      categories: ['电子产品', '服装', '食品', '家居', '其他'],
      percentages: [45, 25, 15, 10, 5]
    },
    message: 'success'
  }
}

// 获取仪表盘数据
export function getDashboardData() {
  if (useMockData) {
    console.log('Using mock dashboard data')
    return Promise.resolve(getMockOverview())
  }
  
  return request({
    url: '/api/dashboard',
    method: 'get'
  })
}

// 获取仪表盘概览数据
export function getOverview() {
  if (useMockData) {
    console.log('Using mock overview data')
    return Promise.resolve(getMockOverview())
  }
  
  return request({
    url: '/api/dashboard',
    method: 'get'
  })
}

// 获取销售趋势数据
export function getSalesTrend(params) {
  if (useMockData) {
    console.log('Using mock sales trend data')
    return Promise.resolve(getMockSalesTrend())
  }
  
  return request({
    url: '/api/dashboard/sales/trend',
    method: 'get',
    params
  })
}

// 获取分类销售数据
export function getCategorySales() {
  if (useMockData) {
    console.log('Using mock category sales data')
    return Promise.resolve(getMockCategorySales())
  }
  
  return request({
    url: '/api/dashboard/sales/category',
    method: 'get'
  })
}

// 获取分类销售占比
export function getSalesPercentage() {
  if (useMockData) {
    console.log('Using mock sales percentage data')
    return Promise.resolve(getMockSalesPercentage())
  }
  
  return request({
    url: '/api/dashboard/sales/percentage',
    method: 'get'
  })
}

// 获取销售统计数据
export function getSalesStats(params) {
  if (useMockData) {
    console.log('Using mock sales stats data')
    return Promise.resolve({
      code: 200,
      data: {
        total: 128750.75,
        growth: 15.8
      },
      message: 'success'
    })
  }
  
  return request({
    url: '/api/dashboard/sales',
    method: 'get',
    params
  })
}

// 获取商品统计数据
export function getProductStats() {
  if (useMockData) {
    console.log('Using mock product stats data')
    return Promise.resolve({
      code: 200,
      data: {
        total: 86,
        active: 75,
        outOfStock: 11
      },
      message: 'success'
    })
  }
  
  return request({
    url: '/api/dashboard/products',
    method: 'get'
  })
}

// 获取订单统计数据
export function getOrderStats() {
  if (useMockData) {
    console.log('Using mock order stats data')
    return Promise.resolve({
      code: 200,
      data: {
        total: 358,
        pending: 12,
        shipped: 25,
        completed: 321
      },
      message: 'success'
    })
  }
  
  return request({
    url: '/api/dashboard/orders',
    method: 'get'
  })
}

// 获取用户统计数据
export function getUserStats() {
  if (useMockData) {
    console.log('Using mock user stats data')
    return Promise.resolve({
      code: 200,
      data: {
        total: 125,
        active: 98,
        newToday: 3
      },
      message: 'success'
    })
  }
  
  return request({
    url: '/api/dashboard/users',
    method: 'get'
  })
} 